package com.hong;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Iterator;
import java.util.List;

public class Test003 {
    public static void main(String[] args) throws DocumentException {
        //1.获取到读取对象
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("D:\\student.xml");
        Element rootElemet = document.getRootElement();
        getNodes(rootElemet);
    }

    public static void getNodes (Element rootElement){
        String name = rootElement.getName();
        System.out.println("节点名称:"+name);
        List<Attribute> attributes = rootElement.attributes();
        for (Attribute attribute:attributes){
            System.out.println("属性名称:"+attribute.getName()+",属性value"+attribute.getValue());;
        }
        String value = rootElement.getText();
        if(!value.equals("")){
            System.out.println("节点值:"+value);
        }

        Iterator<Element> elementIterator = rootElement.elementIterator();
        while (elementIterator.hasNext()){
            Element next = elementIterator.next();
            getNodes(next);
        }
    }
}
